
#ifndef _VIDEO_H_
#define _VIDEO_H_

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>

#define COLOR_FMT_RGB24		4
#define COLOR_FMT_YUV422	13
#define COLOR_FMT_YUV411	14

#define FIELD_FMT_ODD		1<<4
#define FIELD_FMT_EVEN		2<<4
#define FIELD_FMT_ALL		3<<4

#define BTTV_VERSION  _IOR('v' , BASE_VIDIOCPRIVATE+6, int)

typedef unsigned char BYTE;

#include "../DIB/dib.h"

#pragma pack (1)



struct RPLATE0
{
	char result[16];
	int time;
	double fCreditAll,fCredit[16];
	DWORD	charColor,bkColor;
	int index;
	int aveGrayScale;

	int plateWidth;
	int plateHeight;

	char plateBuffer[250*100*3];
};

struct RPLATE
{
	char result[16];
	int time;
	double fCreditAll,fCredit[16];
	DWORD	charColor,bkColor;
	int index;
	int aveGrayScale;

	int plateWidth;
	int plateHeight;

	// 20040216
	CPoint Plate_lefttop;
	CPoint Plate_leftbottom;
	CPoint Plate_righttop;
	CPoint Plate_rightbottom;

	char plateBuffer[250*100*3];
};

struct RPLATE1
{
	char result[16];
	int time;
	double fCreditAll,fCredit[16];
	DWORD	charColor,bkColor;
	int index;
	int aveGrayScale;

	int plateWidth;
	int plateHeight;

	// 20040216
	CPoint Plate_lefttop;
	CPoint Plate_leftbottom;
	CPoint Plate_righttop;
	CPoint Plate_rightbottom;

  	double captime;
};

////////////////////////////
class CCamaraCtrl
{
public:
	//members
	DWORD m_CamaraID[2];	//???????
	DWORD m_CamaraIP;	//???????IP
	int m_channel;	 			//?????
	int m_nVideoCardNo; 		//????
	int m_nMaxCamaraValue;
	int m_nMinCamaraValue;
	int m_nCamaraValue;
	int m_nLastCamaraValue;
	
	int m_bControl; 	//????
	int m_nNoRecongniseNum;
	
	int m_nPlateGray;		//????
	int m_nPlateCount;		//????
	int m_nLastPlateGray;	//????????
	long m_nlLastAdjustTime;	//???????
	long m_nlLastSaveTime;	//????????
	char m_fname[256];
	
	int m_nPlateGrayMaxTH;	//???????
	int m_nPlateGrayMinTH;	//???????
	int m_nPlateCountMaxTH;	//??????
	int m_nAdjustTimeMaxTH;	//????

	int m_nMaxNoPlateCount;
	int m_nMaxVideoGray[3];
	int m_nMinVideoGray[3];
	
	long m_nLastTime;
	void AdjustCamara(); //3, ??????????????????
	void SetCamara();	//??????????

	//functions
	CCamaraCtrl();
	~CCamaraCtrl();	
};
////////////////////////////////////
class CVideo
{
public:
	CVideo();
	~CVideo();
	
	int CalGray(CDIB * pDib);
	
	bool InitVideoSystem(int nIndex,int nColorFmt,int nFieldFmt);
	void CapturePhoto(int nBuffer=0);
	
	double CopyImage(int nBuffer,unsigned char * pBits,bool bConvert=true,bool* pLostData = NULL);
	double CopyJpgImage(int nBuffer,unsigned char* pBits);
	bool ConvertImageFormat(BYTE* ps,BYTE* pd);
	
	void SetChannel(int nIndex);
	
	int	GetBrightness();
	int	GetContrast();
	int	GetHue();
	int	GetSaturation();
	void	SetBrightness(int v);
	void	SetContrast(int v);
	void	SetHue(int v);
	void	SetSaturation(int v);

	///////////camara control ///////////////
	CCamaraCtrl m_Camara;
	BYTE * m_tmppBits;
	bool m_bRunCaptureThread;
	bool m_bQuit;
	pthread_t m_CaptureThread;
	static void* CaptureThread(void*pParam);
	void InitCamara(DWORD CamaraID1=0,DWORD CamaraID2=0,DWORD CamaraIP=0x0100000f,int nMinCamaraValue=0,int nMinCamaraValue=511,char fname[]="/video.dat");
	void ProcessPlate(int nPlateGray);
	bool CaptureImage(int nBuffer);
	bool SyncCapture(int nBuffer);
	
	/////////////////////////////////////////
private:
	
	int   m_fd;
	void* m_map;	
	
	struct video_mmap v[2];
	
	double m_cpu_hz;
	
	int	m_nColorFmt;
	int	m_nFieldFmt;
	
	struct video_picture m_videoParam;
	
	void	GetVideoParam();
	void	SetVideoParam();
		
	void ConvertFromYUV422(BYTE* pData,BYTE* pDib);
	void ConvertFromYUV411(BYTE* pData,BYTE* pDib);
	
	pthread_mutex_t m_CaptureMutex;
};

#endif

